package com.vlong.town.controller.admin.goodsType;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.vlong.town.controller.base.BaseController;
import com.vlong.town.entity.base.DataMessage;
import com.vlong.town.entity.goodsType.GoodsType;
import com.vlong.town.service.goodsType.GoodsTypeService;
import com.vlong.town.util.common.TownConstant;

@Controller
@RequestMapping("/goodsType")
public class GoodsTypeController extends BaseController{

	@Autowired
	private GoodsTypeService  goodsTypeService;//特产业务接口
	
	/**
	 * 跳转特产分类页面
	 * @return
	 */
	@RequestMapping("/list")
	public String list(){
		return  "admin/goodsType/list_goodsType";
	}
	
	
	/**
	 * 查询分类
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/treeTable")
	public  DataMessage  treeTable(){
		// 返回数据消息实体
		DataMessage dm = new DataMessage();
		List<GoodsType> resultList = new ArrayList<GoodsType>();
		List<GoodsType> goodsTypeList = goodsTypeService.queryList(null, null, null, 1, null, null, TownConstant.DEL_FLAG_0, null, null, null, null);
        for(GoodsType goodsType : goodsTypeList) {
			if(goodsType.getTypeLevel().intValue() == 1) {
				this.recursionSubGoodsType(resultList, goodsType);
			}
		}
        dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
		dm.setData(resultList);
		return  dm;
	}
	
	/**
	 * 递归添加分类子集
	 * @param resultList 结果List
	 * @param parentGoodsType 父级分类
	 */
	private void recursionSubGoodsType(List<GoodsType> resultList, GoodsType goodsType) {
		if(goodsType.getIsParent().intValue() == 1) {
			List<GoodsType> subList = goodsTypeService.querySubList(goodsType.getTypeId());
			goodsType.setSubList(subList);
			resultList.add(goodsType);
			for(GoodsType type : subList) {
				if(type.getIsParent().intValue() == 1) {
					recursionSubGoodsType(resultList, type);
				}
			}
		} else {
			resultList.add(goodsType);
		}
	}
	
	/**
	 * 跳转添加跟特产页面
	 * @return
	 */
	@RequestMapping("/addGoodsType")
	public  String  addGoodsType(){
		return  "admin/goodsType/add_goodsType";
	}
	
	
	/**
     * 添加分类
     * @param typeName
     * @param typeDesc
     * @return
     */
    @ResponseBody
    @RequestMapping("/executeAddGoodsType")
    public  DataMessage  executeAddGoodsType(
    		@RequestParam(required=false, value="typeName") String typeName,
    		@RequestParam(required=false, value="typeDesc") String typeDesc
    		){
    	DataMessage dm=new DataMessage();
    	if(!this.validateStringProperty(typeName, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("分类名称不合法");
        }else{
        	this.goodsTypeService.add(null, typeName, 0L, 1, null, 1, TownConstant.DEL_FLAG_0, 1, typeDesc, System.currentTimeMillis(), null);
        	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("分类添加成功");
        }
    	return  dm;
    }
	
    
    /**
	 * 跳转添加子级分类页面
	 * @return
	 */
	@RequestMapping("/addSubGoodsType")
	public  String  addSubGoodsType(@RequestParam(required=false, value="TypePid") String TypePid){
		request.setAttribute("TypePid", TypePid);
		return  "admin/goodsType/add_sub_goodsType";
	}
    
	
	/**
     * 添加分类
     * @param typeName
     * @param typeDesc
     * @return
     */
    @ResponseBody
    @RequestMapping("/executeAddSubGoodsType")
    public  DataMessage  executeAddSubGoodsType(
    		@RequestParam(required=false, value="typePid") String typePid,
    		@RequestParam(required=false, value="typeName") String typeName,
    		@RequestParam(required=false, value="typeDesc") String typeDesc
    		){
    	DataMessage dm=new DataMessage();
    	if(!this.validateStringProperty(typeName, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("分类名称不合法");
        }else{
        	GoodsType goodsType = goodsTypeService.queryById(Long.parseLong(typePid));
        	if(goodsType!=null){
        		this.goodsTypeService.add(null, typeName, Long.parseLong(typePid), 1, null, goodsType.getTypeLevel()+1, TownConstant.DEL_FLAG_0, 0, typeDesc, System.currentTimeMillis(), null);
        		if(goodsType.getIsParent().intValue()==0){
        			goodsTypeService.editIsParent(goodsType.getTypeId(), 1);
        		}
        	}
        	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("分类添加成功");
        }
    	return  dm;
    }
	
    @RequestMapping("/editGoodsType")
    public  String  editGoodsType(@RequestParam(required=false, value="typeId") String typeId){
    	GoodsType goodsType = goodsTypeService.queryById(Long.parseLong(typeId));
    	request.setAttribute("goodsType", goodsType);
    	return  "admin/goodsType/edit_goodsType";
    }
    
    
    /**
     * 修改
     * @param typeId
     * @param typeName
     * @param typeDesc
     * @return
     */
    @ResponseBody
    @RequestMapping("/executeEditGoodsType")
    public  DataMessage  executeEditGoodsType(
    		@RequestParam(required=false, value="typeId") String typeId,
    		@RequestParam(required=false, value="typeName") String typeName,
    		@RequestParam(required=false, value="typeDesc") String typeDesc
    		){
    	DataMessage dm=new DataMessage();
    	if(!this.validateStringProperty(typeName, 1, 50)) {
            dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
            dm.setMessage("分类名称不合法");
        }else{
        	this.goodsTypeService.edit(Long.parseLong(typeId), typeName, null, null, null, null, null, null, typeDesc, null, System.currentTimeMillis());
        	dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
            dm.setMessage("分类修改成功");
        }
    	return  dm;
    }
    
    /**
     * 删除分类
     * @param typeId  分类id
     * @return DataMessage
     */
     @ResponseBody
     @RequestMapping("/delete")
     public  DataMessage  delete(@RequestParam(required=false, value="typeId") Long typeId){
     	DataMessage dm = new DataMessage();
     	if(typeId != null && typeId > 0L) {
     		goodsTypeService.editIsDel(typeId, TownConstant.DEL_FLAG_1);
             dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
             dm.setMessage("分类删除成功");
         } else {
             dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
             dm.setMessage("参数错误");
         }
         return dm;
     }
    
     
     /**
      * 批量删除分类
      * @param typeIds
      * @return
      */
     @ResponseBody
     @PostMapping("/batchDelete")
     public DataMessage batchDeleteUser(@RequestParam(required=true, value="typeIds") String typeIds) {
         DataMessage dm = new DataMessage();
         if(this.validateStringProperty(typeIds, 1, 128)) {
             String[] typeIdsArray = typeIds.split(",");
             for(String typeId : typeIdsArray) {
            	 goodsTypeService.editIsDel(Long.parseLong(typeId), TownConstant.DEL_FLAG_1);
             }
             dm.setResult(TownConstant.RESULT_CODE_SUCCESS);
             dm.setMessage("分类删除成功");
         } else {
             dm.setResult(TownConstant.RESULT_CODE_PARAMETER_ERROR);
             dm.setMessage("参数错误");
         }
         return dm;
     }
	
}
